package com.ruoyi.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.entity.Project;
import com.ruoyi.common.entity.ProjectItem;
import com.ruoyi.common.entity.dto.CourseCatalogDTO;
import com.ruoyi.common.entity.dto.ProjectDetailDTO;
import com.ruoyi.common.entity.dto.ProjectIntroDTO;
import com.ruoyi.common.mapper.ProjectMapper;
import com.ruoyi.common.service.ProjectItemService;
import com.ruoyi.common.service.ProjectService;
import lombok.RequiredArgsConstructor;
import org.joda.time.LocalDate;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    private final ProjectItemService projectItemService;

    @Cacheable(value = "projectDetailCache", key = "#id")
    @Override
    public ProjectDetailDTO getProjectDetail(Long id) {
        ProjectItem projectItem = projectItemService.selectById(id);
        Project project = this.baseMapper.selectById(projectItem.getProjectId());

        ProjectDetailDTO dto = new ProjectDetailDTO();
        dto.setId(project.getId());
        dto.setTitle(project.getTitle());
        dto.setDate(project.getCreateTime().toString()); // 格式化日期
        dto.setTag(project.getTag() != null ? project.getTag() : "实战项目");
        dto.setContentTitle(project.getContentTitle());
        dto.setResourceUrl(project.getResourceUrl());

        // 项目介绍
        ProjectIntroDTO intro = new ProjectIntroDTO();
        intro.setTitle(project.getIntroTitle());
        intro.setDescription(project.getIntroDescription());
        intro.setHighlight1(project.getIntroHighlight1());
        intro.setMiddleText(project.getIntroMiddleText());
        intro.setHighlight2(project.getIntroHighlight2());
        intro.setEndText(project.getIntroEndText());
        dto.setProjectIntro(intro);

        // 课程目录
        CourseCatalogDTO catalog = new CourseCatalogDTO();
        catalog.setTitle(project.getCatalogTitle());
        catalog.setItems(projectItemService.findByProjectId(id)); // 从关联表查询
        dto.setCourseCatalog(catalog);

        return dto;
    }

    @Override
    public void insertSpiderProjects(List<Project> projectList) {

    }

    @Override
    public String getProjectByIdBaidu(Long id) {
        Project project = this.baseMapper.selectOne(Wrappers.<Project>lambdaQuery()
                .select(Project::getResourceUrl)
                .eq(Project::getId, id));
        return project.getResourceUrl();
    }

    @Override
    public boolean deleteProject(Long id) {
        if (this.baseMapper.deleteById(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean updateProject(Project project) {
        Project project1 = this.baseMapper.selectById(project.getId());
        if (ObjectUtil.isNull(project1)){
            throw new RuntimeException("项目不存在");
        }
        return this.baseMapper.updateById(project) > 0;
    }

    @Override
    public boolean addProject(Project project) {
        project.setCreateTime(LocalDateTime.now());
        if (this.baseMapper.insert(project) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Project getProjectById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public IPage<Project> listProjects(Page<Project> page, Map<String, Object> query) {
        return null;
    }
    // 继承ServiceImpl后，拥有了基本的CRUD能力
    // 可以在这里实现自定义的业务逻辑
}